/* eslint-disable no-console */
/**
 * request 网络请求工具
 * 更详细的 api 文档: https://github.com/umijs/umi-request
 */
import { extend } from "umi-request";
import { getQueryString } from './utils';
/**
 * 异常处理程序
 */
const errorHandler = (error) => {
  const { response } = error;
  if (response && response.status) {
    if (response.status === 401) {
      return response;
    }
  }
  return response;
};

/**
 * 配置request请求时的默认参数
 */
const request = extend({
  prefix: 'http://118.24.5.129:8102/wjgl',
  errorHandler, // 默认错误处理
});

request.interceptors.request.use((url, options) => {
  const token = getQueryString('token') || window.localStorage.getItem("token");
  if (token && token !== "null" && token !== "undefined") {
    const headers = {
      'token': token,
    };
    return {
      url,
      options: { ...options, headers },
    }
  }
});

// response拦截器, 处理response
request.interceptors.response.use(async response => {
  return response;
});

export default request;
